import app from '../../bin/server'
import supertest from 'supertest'
import { expect, should } from 'chai'
import { cleanDb, authTestUser } from '../utils'

should()
const request = supertest.agent(app.listen())
const context = {}

describe('问题相关api', () => {
  before((done) => {
    // cleanDb()
    console.log('在进行测试前，通过测试账号获取授权')
    authTestUser(request, (err, { user, token }) => {
      if (err) {
        return done(err)
      }
      console.log('获取授权成功')
      context.user = user
      context.token = token
      done()
    })
  })

    it('成功创建一条题目', (done) => {
      const { token } = context
      request
        .post('/questions')
        .set({ Accept: 'application/json', Authorization: `Bearer ${token}` })
        .send({
          question: {
            _creator:1,
            questions_terms_id: '582028f120b88f18945a1558',
            questions_name: 'this is a  subTitle',
            questoins_options:['这是选项1','这是选项2'],
            questions_true_answers:[0,1]
          }
        })
        .expect(200, (err, res) => {
          if (err) {
            return done(err)
          }
          res.body.should.have.property('question')
          done()
        })
    })

  

    it('获取所有问题', (done) => {
      const { token } = context
      request
        .get('/questions')
        .set({ Accept: 'application/json', Authorization: `Bearer ${token}` })

        .expect(200, (err, res) => {
          if (err) {
            return done(err)
          }
          res.body.should.have.property('questions')
          done()
        })
    })
 
})
